package db

import (
	"fmt"

	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

var (
	Url = "root:root@/test?charset=utf8&parseTime=True&loc=Local"
	db  *gorm.DB
)

func Init() {
	database, err := gorm.Open("mysql", Url)
	if err != nil {
		panic("failed to connect database")
	}
	//defer db.Close()
	db = database
}

type User struct {
	gorm.Model
	Username string
	Nickname string
	Password string
}

func QueryUser(id uint) {
	var user User
	db.First(&user, id)
	fmt.Println(user)
}

func CreateUser(user *User) uint {
	db.Create(user)
	return user.ID
}
